package commands;

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import vo.Comment;
import vo.Photo;
import vo.User;
import vo.Vote;

public class CreateTablesCommand implements Comando {

	@Override
	public String execute(String[] args) throws IOException {
		
		if (args.length != 1) {
			return getSintaxe();
		}

		String saida = "";
		saida  = this.criarTabela(User.TABLENAME, User.CF_ATTRIBUTES);
		saida += this.criarTabela(Photo.TABLENAME, Photo.CF_ATTRIBUTES);
		saida += this.criarTabela(Comment.TABLENAME, Comment.CF_ATTRIBUTES);
		saida += this.criarTabela(Vote.TABLENAME, Vote.CF_ATTRIBUTES);
		return saida;

	}

	private String criarTabela(String tabela, String cf) throws IOException {
		HBaseAdmin hBaseAdmin = new HBaseAdmin(HBaseConfiguration.create());
		if (hBaseAdmin.tableExists(tabela)) {
			return "Tabela " + tabela + " já existe\n";
		}

		HTableDescriptor descriptor = new HTableDescriptor(tabela);
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf);
		descriptor.addFamily(columnDescriptor);
		hBaseAdmin.createTable(descriptor);
		
		return tabela+"\n";
	}

	@Override
	public String getKey() {
		return "-createTables";
	}

	@Override
	public String getSintaxe() {
		return "-createTables";
	}

}
